define(["jquery", "easy-admin"], function ($, ea) {
    $('#bill_amount').blur(function () {
        capitaChange()
    })

    $('#capital_amount').blur(function () {
        capitaChange()
    })

    $('#arrival_amount').blur(function () {
        capitaChange()
    })

    function capitaChange(params) {
        var capital_amount = $("#capital_amount").val()
        var arrival_amount = $("#arrival_amount").val()
        if (capital_amount > 0) {
            var service_charge = capital_amount - arrival_amount
            if (service_charge > 0) {
                $("#service_charge").val(service_charge.toFixed(2))
            } else {
                layer.msg("到账金额>周转金额，请仔细核对")
                $("#service_charge").val("")
            }
        } else {
            $("#capital_amount").val("0")
            $("#service_charge").val("0")
        }
        supplementChange();
    }

    function supplementChange() {
        var bill_amount = $("#bill_amount").val()
        var arrival_amount = $("#arrival_amount").val()
        var service_charge = $("#service_charge").val()
        var supplement_amount = (bill_amount - arrival_amount).toFixed(2) - service_charge
        if (supplement_amount < 0) {
            layer.msg("补充金额错误，请仔细核对")
            supplement_amount = 0;
        }
        $("#supplement_amount").val(supplement_amount.toFixed(2))

        var supplement_sum = bill_amount - arrival_amount
        if (supplement_sum < 0) {
            supplement_sum = 0;
        }
        $("#supplement_sum").val(supplement_sum.toFixed(2))
    }

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'book.capital_turnover/index',
        add_url: 'book.capital_turnover/add',
        edit_url: 'book.capital_turnover/edit',
        delete_url: 'book.capital_turnover/delete',
        export_url: 'book.capital_turnover/export',
        modify_url: 'book.capital_turnover/modify',
        recycle_url: 'book.capital_turnover/recycle',
    };

    return {

        index: function () {
            ea.table.render({
                init: init,
                totalRow: true,
                cols: [[
                    { type: 'checkbox' },
                    { field: 'id', title: 'ID',width:100, search:false  , totalRow: '合计：'},
                    { field: 'name', title: '周转项目', selectList:  {'平安':'平安','招商':'招商','中信':'中信','浦发':'浦发','交通2749':'交通2749','交通5316':'交通5316','花呗':'花呗','白条':'白条','其他':'其他'},  searchOp:"%*%"},
                    { field: 'date', title: '周转日期', search:false },
                    { field: 'bill_amount', title: '账单金额', search:false },
                    { field: 'capital_amount', title: '周转金额', search:false },
                    { field: 'arrival_amount', title: '到账金额', search:false },
                    { field: 'service_charge', title: '手续费', search:false, totalRow: '{{= d.TOTAL_ROW.serviceCharge}} 元'},
                    { field: 'supplement_amount', title: '补充金额' , search:false},
                    { field: 'supplement_sum', title: '补充总计', search:false, totalRow: '{{= d.TOTAL_ROW.supplementSum}} 元'},
                    { field: 'remark', title: '备注', search:false },
                    { width: 250, title: '操作', templet: ea.table.tool },

                ]],
            });

            ea.listen();
        },
        add: function () {
            ea.listen();
        },
        edit: function () {
            ea.listen();
        },
        recycle: function () {
            init.index_url = init.recycle_url;
            ea.table.render({
                init: init,
                toolbar: ['refresh',
                    [{
                        class: 'layui-btn layui-btn-sm',
                        method: 'get',
                        field: 'id',
                        icon: 'fa fa-refresh',
                        text: '全部恢复',
                        title: '确定恢复？',
                        auth: 'recycle',
                        url: init.recycle_url + '?type=restore',
                        checkbox: true
                    }, {
                        class: 'layui-btn layui-btn-danger layui-btn-sm',
                        method: 'get',
                        field: 'id',
                        icon: 'fa fa-delete',
                        text: '彻底删除',
                        title: '确定彻底删除？',
                        auth: 'recycle',
                        url: init.recycle_url + '?type=delete',
                        checkbox: true
                    }], 'export',
                ],
                cols: [[
                    { type: 'checkbox' },
                    { field: 'id', title: 'ID' },
                    { field: 'name', title: '周转项目' },
                    { field: 'date', title: '周转日期' },
                    { field: 'bill_amount', title: '账单金额' },
                    { field: 'capital_amount', title: '周转金额' },
                    { field: 'arrival_amount', title: '到账金额' },
                    { field: 'service_charge', title: '手续费' },
                    { field: 'supplement_amount', title: '补充金额' },
                    { field: 'supplement_sum', title: '补充总计' },

                    {
                        width: 250,
                        title: '操作',
                        templet: ea.table.tool,
                        operat: [
                            [{
                                title: '确认恢复？',
                                text: '恢复数据',
                                filed: 'id',
                                url: init.recycle_url + '?type=restore',
                                method: 'get',
                                auth: 'recycle',
                                class: 'layui-btn layui-btn-xs layui-btn-success',
                            }, {
                                title: '想好了吗？',
                                text: '彻底删除',
                                filed: 'id',
                                method: 'get',
                                url: init.recycle_url + '?type=delete',
                                auth: 'recycle',
                                class: 'layui-btn layui-btn-xs layui-btn-normal layui-bg-red',
                            }]]
                    }
                ]],
            });

            ea.listen();
        },
    };
});